<template>
  <div class="dormitory-approval">
    <div class="header">
      <router-link to="/main/wuye/dormitory/process" class="back-btn">
        <i class="el-icon-arrow-left"></i>
        <span>返回</span>
      </router-link>
      <h2>审批分配</h2>
    </div>

    <div class="content">
      <el-tabs v-model="activeTab">
        <el-tab-pane label="待审批" name="pending">
          <el-table :data="pendingList" style="width: 100%">
            <el-table-column prop="applyNo" label="申请编号" width="180"></el-table-column>
            <el-table-column prop="applicant" label="申请人" width="120"></el-table-column>
            <el-table-column prop="department" label="所属部门" width="150"></el-table-column>
            <el-table-column prop="applyType" label="申请类型" width="120"></el-table-column>
            <el-table-column prop="roomType" label="宿舍类型" width="120"></el-table-column>
            <el-table-column prop="expectedTime" label="期望入住时间" width="180"></el-table-column>
            <el-table-column label="操作" width="200">
              <template slot-scope="scope">
                <el-button size="mini" @click="viewDetail(scope.row)">查看</el-button>
                <el-button size="mini" type="primary" @click="handleApproval(scope.row)">审批</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>

        <el-tab-pane label="已审批" name="completed">
          <el-table :data="completedList" style="width: 100%">
            <el-table-column prop="applyNo" label="申请编号" width="180"></el-table-column>
            <el-table-column prop="applicant" label="申请人" width="120"></el-table-column>
            <el-table-column prop="approvalResult" label="审批结果" width="120">
              <template slot-scope="scope">
                <el-tag :type="scope.row.approvalResult === '通过' ? 'success' : 'danger'">
                  {{ scope.row.approvalResult }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="roomNo" label="分配宿舍" width="120"></el-table-column>
            <el-table-column prop="approvalTime" label="审批时间" width="180"></el-table-column>
            <el-table-column label="操作" width="120">
              <template slot-scope="scope">
                <el-button size="mini" @click="viewDetail(scope.row)">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </div>

    <!-- 详情对话框 -->
    <el-dialog title="申请详情" :visible.sync="detailDialogVisible" width="60%">
      <div class="application-detail" v-if="currentApplication">
        <el-descriptions title="基本信息" :column="2" border>
          <el-descriptions-item label="申请编号">{{ currentApplication.applyNo }}</el-descriptions-item>
          <el-descriptions-item label="申请人">{{ currentApplication.applicant }}</el-descriptions-item>
          <el-descriptions-item label="所属部门">{{ currentApplication.department }}</el-descriptions-item>
          <el-descriptions-item label="职位">{{ currentApplication.position }}</el-descriptions-item>
          <el-descriptions-item label="联系电话">{{ currentApplication.phone }}</el-descriptions-item>
          <el-descriptions-item label="申请类型">{{ currentApplication.applyType }}</el-descriptions-item>
          <el-descriptions-item label="宿舍类型">{{ currentApplication.roomType }}</el-descriptions-item>
          <el-descriptions-item label="预计住宿时长">{{ currentApplication.duration }}月</el-descriptions-item>
        </el-descriptions>

        <div class="detail-section">
          <h4>申请原因</h4>
          <p>{{ currentApplication.reason }}</p>
        </div>

        <div class="detail-section" v-if="currentApplication.approvalInfo">
          <h4>审批信息</h4>
          <el-descriptions :column="2" border>
            <el-descriptions-item label="审批结果">{{ currentApplication.approvalInfo.result }}</el-descriptions-item>
            <el-descriptions-item label="审批人">{{ currentApplication.approvalInfo.approver }}</el-descriptions-item>
            <el-descriptions-item label="审批时间">{{ currentApplication.approvalInfo.time }}</el-descriptions-item>
            <el-descriptions-item label="分配宿舍">{{ currentApplication.approvalInfo.roomNo }}</el-descriptions-item>
          </el-descriptions>
          <p class="approval-comment">审批意见：{{ currentApplication.approvalInfo.comment }}</p>
        </div>
      </div>
    </el-dialog>

    <!-- 审批对话框 -->
    <el-dialog title="宿舍审批" :visible.sync="approvalDialogVisible" width="50%">
      <el-form :model="approvalForm" label-width="100px">
        <el-form-item label="审批结果">
          <el-radio-group v-model="approvalForm.result">
            <el-radio label="通过">通过</el-radio>
            <el-radio label="拒绝">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="分配宿舍" v-if="approvalForm.result === '通过'">
          <el-select v-model="approvalForm.roomNo" placeholder="请选择宿舍">
            <el-option
              v-for="room in availableRooms"
              :key="room.roomNo"
              :label="room.roomNo + ' (' + room.building + ')'"
              :value="room.roomNo">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="审批意见">
          <el-input type="textarea" v-model="approvalForm.comment" rows="4"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="approvalDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitApproval">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeTab: 'pending',
      pendingList: [],
      completedList: [],
      detailDialogVisible: false,
      approvalDialogVisible: false,
      currentApplication: null,
      approvalForm: {
        result: '通过',
        roomNo: '',
        comment: ''
      },
      availableRooms: [
        { roomNo: '101', building: 'A栋' },
        { roomNo: '102', building: 'A栋' },
        { roomNo: '201', building: 'B栋' },
        { roomNo: '202', building: 'B栋' }
      ]
    }
  },
  created() {
    // TODO: 从后端获取待审批和已审批列表
    this.pendingList = [
      {
        applyNo: 'SS202403110001',
        applicant: '张三',
        department: '技术部',
        applyType: '新员工申请',
        roomType: '双人间',
        expectedTime: '2024-03-15'
      }
    ]
  },
  methods: {
    viewDetail(row) {
      this.currentApplication = row
      this.detailDialogVisible = true
    },
    handleApproval(row) {
      this.currentApplication = row
      this.approvalForm = {
        result: '通过',
        roomNo: '',
        comment: ''
      }
      this.approvalDialogVisible = true
    },
    submitApproval() {
      if (!this.approvalForm.comment) {
        this.$message.error('请填写审批意见')
        return
      }
      if (this.approvalForm.result === '通过' && !this.approvalForm.roomNo) {
        this.$message.error('请选择分配的宿舍')
        return
      }

      // TODO: 提交审批结果到后端
      const approvalInfo = {
        result: this.approvalForm.result,
        roomNo: this.approvalForm.roomNo,
        comment: this.approvalForm.comment,
        approver: '管理员',
        time: new Date().toLocaleString()
      }

      this.currentApplication.approvalInfo = approvalInfo
      this.currentApplication.approvalResult = approvalInfo.result
      this.currentApplication.roomNo = approvalInfo.roomNo
      this.currentApplication.approvalTime = approvalInfo.time

      // 从待审批列表移动到已审批列表
      const index = this.pendingList.findIndex(item => item.applyNo === this.currentApplication.applyNo)
      if (index > -1) {
        this.pendingList.splice(index, 1)
        this.completedList.unshift(this.currentApplication)
      }

      this.$message.success('审批提交成功')
      this.approvalDialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.dormitory-approval {
  padding: 20px;

  .header {
    margin-bottom: 30px;
    position: relative;
    text-align: center;

    .back-btn {
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      text-decoration: none;
      color: #606266;
      display: flex;
      align-items: center;
    }

    h2 {
      margin: 0;
      font-size: 24px;
      color: #303133;
    }
  }

  .content {
    background: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  }

  .detail-section {
    margin-top: 20px;

    h4 {
      margin: 0 0 10px;
      color: #303133;
    }

    p {
      margin: 0;
      color: #606266;
      line-height: 1.6;
    }

    .approval-comment {
      margin: 5px 0 0;
      font-size: 13px;
      color: #909399;
    }
  }
}
</style> 